public class Demo1 {
    public static void main(String[] args) {
        Thread t3 = new Thread(() -> {
            System.out.print(Thread.currentThread().getName() + " ");
        }, "c");
        Thread t2 = new Thread(() -> {
            try {
                t3.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.print(Thread.currentThread().getName() + " ");
        }, "b");
        Thread t1 = new Thread(() -> {
            try {
                t2.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.print(Thread.currentThread().getName() + " ");
        }, "a");

        t1.start();
        t2.start();
        t3.start();
    }
    public static void main2(String[] args) {
        Object locker1 = new Object();
        Object locker2 = new Object();
        Thread t1 = new Thread(() -> {
            synchronized (locker1){
                try {
                    locker1.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.print(Thread.currentThread().getName() + " ");
        }, "a");
        Thread t2 = new Thread(() -> {
            synchronized (locker2){
                try {
                    locker2.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.print(Thread.currentThread().getName() + " ");
            synchronized (locker1){
                locker1.notify();
            }
        }, "b");
        Thread t3 = new Thread(() -> {
            System.out.print(Thread.currentThread().getName() + " ");
            synchronized (locker2){
                locker2.notify();
            }
        }, "c");

        t1.start();
        t2.start();
        t3.start();
    }
    public static void main1(String[] args) throws InterruptedException {
        Object locker1 = new Object();
        Object locker2 = new Object();
        Object locker3 = new Object();
        Thread t1 = new Thread(() -> {
            synchronized (locker1){
                try {
                    locker1.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.print(Thread.currentThread().getName() + " ");
            synchronized (locker3){
                locker3.notify();
            }
        }, "a");
        Thread t2 = new Thread(() -> {
            synchronized (locker2){
                try {
                    locker2.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.print(Thread.currentThread().getName() + " ");
            synchronized (locker1){
                locker1.notify();
            }
        }, "b");
        Thread t3 = new Thread(() -> {
            synchronized (locker3){
                try {
                    locker3.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.print(Thread.currentThread().getName() + " ");
            synchronized (locker2){
                locker2.notify();
            }
        }, "c");

        t1.start();
        t2.start();
        t3.start();
        Thread.sleep(1000);
        synchronized (locker3){
            locker3.notify();
        }
    }
}